(function() {
    'use strict';

	var flag = getUrlParms("flag");
    //默认为获取内设机构信息
	var isOrgan = true;
	var html = '<div class="title">内设机构</div><div class="organ-main clearfix">' +
		'<div class="fl organ-main-btn option-div"></div><div class="fl organ-main-content" id="di"></div></div>';
    //惠民政策
	if("beneficiary"==flag){
		html = '<div class="layui-tab layui-tab-brief" lay-filter="beneficiaryType">' +
			'<ul class="layui-tab-title" style="float: left;">\n' +
            '                    <li lay-id="0">全部类型</li>\n' +
            '                    <li lay-id="1">农村低保</li>\n' +
            '                    <li lay-id="2">城市低保</li>\n' +
            '                    <li lay-id="3">特困供养</li>\n' +
            '                    <li lay-id="4">优抚对象</li>\n' +
            '                    <li lay-id="5">高龄老人</li>\n' +
        	'                    <li lay-id="6">救济救助</li>\n' +
            '</ul>' +
            '<div class="layui-inline" style="margin: 2px 2px 2px 15px;' +
            '        <div class="layui-input-inline small-input">\n' +
            '            <input type="text" placeholder="请输入名字" class="layui-input search_input">\n' +
            '        </div>' +
            '        <a class="layui-btn search_btn">查询</a>' +
            '    </div>'+
            '</div>' +
			'<div style="height: 680px">' +
            '<div class="village-div"></div><div><table class="layui-hide" id="beneficiaryTableId" lay-filter="beneficiaryTable"></table></div></div>';
		isOrgan = false;
	}

	layui.use([ "jquery",'element','layer','table' ], function() {
		var $ = layui.$
            ,table = layui.table//表格
			,element = layui.element//事件
			,layer = layui.layer;//弹出层

        $(".organ").html(html);
		if(isOrgan){
            //获取机构职能
            getOrgan($);
		}else{
			//获取惠民信息
			var index = getUrlParms("index");
			index = isNull(index)?'0':index;
            element.tabChange('beneficiaryType', index); //切换到相应lay-id类型
            //获取乡镇信息
            getVillageInfo($,'');
            //执行一个 table 实例
            var type = index;//默认是这个类型;
            var village;
            getBeneficiaryInfo(type,'');
            element.on('tab(beneficiaryType)', function(data){
                var newType = $(this).attr("lay-id");
                if(newType==type){
                    return;
                }
                type = newType;
                village = $(".village-div").find("#checkedButton").attr("itemid");
                //layer.msg("切换了"+type+","+village);
                //重新加载一个表格
                getBeneficiaryInfo(type,village);
            });
		}

		$("#app .organ .organ-main button").on("click", function() {
			$("#app .organ .organ-main .organ-main-content>div").eq($(this).index()).show().siblings().hide();
            var html = " <div style='padding: 5px;box-sizing: border-box;'><h1 style='text-align: center;'>" +
                "<font style='font-size:200%;'>暂无相关内容</font></h1></div>"
            var content = $(this).data("optionContent");
            if(!isNull(content)){
                html = content;
            }
            $("#di").html(html);
		});$("#app .organ .organ-main button").first().click();
		//单击乡镇按钮,添加样式,并调用后台重新获取表格数据
        $("#app .organ .village-div button").on('click',function(){
            //var background = $(this).attr("style");
            var thisId = $(this).attr("id");
            if(!isNull(thisId)){
                return;
            }
            $(".village-div").find("#checkedButton").removeAttr("id");
            $(this).attr("id","checkedButton");
            //$(".village-div button").addClass("checked");
            // $(".village-div").find("#checkedButton").removeAttr("id");
            // $(this).removeClass("checked");
            // $(this).attr("id","checkedButton");
            var villageId = $(this).attr("itemid");
            var type = $(".layui-tab-title").find(".layui-this").attr("lay-id");
            //重新加载一个表格
            getBeneficiaryInfo(type,villageId);
        });

		//根据姓名在受惠类型和乡镇范围内查询受惠人
        $(".search_btn").on("click",function () {
            var searchName = $(".search_input").val();
            if(isNull(searchName)){
                layer.msg("请输入要搜索的名字");
                return;
            }
            getBeneficiaryInfo(type,village,searchName);
        });
	});
}());

//1.获取机构职能
function getOrgan($){
    $.ajax({
        url : "/CivilAffairs/option/getOptionsByType",
        type : "post",
        data : {"optionType":"1"},
        async : false,
        dataType : "json",
        success : function(result) {
            if(result.status!="0000"){
                $(".option-div").html(result.detail)
            }else{
                var options = result.data;
                for(var i=0;i<options.length;i++){
                    var button = '<button><a href=\"javascript:;\"><i class=\"fa fa-file-text-o\" aria-hidden=\"true\">' +
                        '</i>&nbsp;&nbsp;'+options[i].option_name+'</a></button>';
                    var $button	 = $(button);
                    $button.data("optionId",options[i].option_id);
                    $button.data("optionContent",options[i].option_content);
                    $(".option-div").append($button);
                }
            }
        },
        error : function(result) {
            console.log("获取机构职能异常:"+result.status+","+result.statusMessage);
            alert("系统异常,请稍后重试");
        }
    });
}
//获取受惠人信息
function getBeneficiaryInfo(beneficiaryType,villageId,searchName){
    beneficiaryType = 0==beneficiaryType?"":beneficiaryType;
    searchName = isNull(searchName)?"":searchName;
    layui.use(['table'],function () {
    	var table = layui.table;
        //执行一个 table 实例
        table.render({
            elem: '#beneficiaryTableId'
            ,height: 666
            //,height: 'full-70' //高度最大化减去差值
            //,cellMinWidth: 80 //列宽自适应,最小宽度为80
            ,limit: 15
            ,even: true //开启隔行背景
            ,url: '/CivilAffairs/beneficiary/getBeneficiaryInfo' //数据接口
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'beneficiary_num', title: '序号', sort: true, fixed: 'left',width:200}
                ,{field: 'beneficiary_name', title: '姓名',width:120}
                ,{field: 'standards', title: '标准',width:200}
                // ,{field: 'city', title: '城市', width:80}
                ,{field: 'address', title: '住址'}
                // ,{field: 'experience', title: '积分', width: 80, sort: true}
                // ,{field: 'score', title: '评分', width: 80, sort: true}
                // ,{field: 'classify', title: '职业', width: 80}
                // ,{field: 'wealth', title: '财富', width: 135, sort: true}
                //,{fixed: 'right',title:"操作",align:'center', toolbar: '#barDemo'}
            ]]
			,where: {beneficiaryType: beneficiaryType, villageId: villageId,searchName:searchName}//如果无需传递额外参数，可不加该参数
            ,method: 'post' //如果无需自定义HTTP类型，可不加该参数
            // ,response: {
            //     statusName: 'status' //数据状态的字段名称，默认：code
            //     ,statusCode: '0000' //成功的状态码，默认：0
            //     //,msgName: 'hint' //状态信息的字段名称，默认：msg
            //     //,countName: 'total' //数据总数的字段名称，默认：count
            //     //,dataName: 'rows' //数据列表的字段名称，默认：data
            // }
        });
    });
}

/**
 * 获取乡镇信息
 * @param villageId
 */
function getVillageInfo($,villageId){
    $.ajax({
        url : "/CivilAffairs/village/getVillageInfo",
        type : "post",
        data : {"villageId":villageId},
        async : false,
        dataType : "json",
        success : function(result) {
            $(".village-div").html(result.data);
        },
        error:function(result){
            console.log("获取乡镇信息异常:"+result.status+","+result.statusMessage);
            alert("系统异常,请稍后重试");
        }
    });
}